package com.fanketly.musicdiary.test

import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.material.Slider
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.*
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.LineBreak
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.fanketly.musicdiary.ui.theme.TestTheme
import com.fanketly.musicdiary.ui.theme.White

@Preview
@Composable
fun TextLineBreakingPreview() {
    TestTheme {
        Surface(color = White) {
            TextLineBreakingDemo()
        }
    }
}


/**
 *Version 1.3.0-beta03 添加选项以自定义文本中的换行符。
 **/

@OptIn(ExperimentalTextApi::class)
private val lineBreakOptions = listOf(
    //贪婪、快速的换行算法。非常适合经常更新的文本，例如文本编辑器，因为文本将重新排列最少
    "Simple" to LineBreak.Simple,
    //更慢、更高质量的换行以提高可读性。适用于大量文本。
    "Paragraph" to LineBreak.Paragraph,
    //平衡的行长、连字符和基于短语的中断。适用于短文本，例如标题或狭窄的报纸专栏
    "Heading" to LineBreak.Heading,
    "Custom" to LineBreak(
        strategy = LineBreak.Strategy.Balanced,
        strictness = LineBreak.Strictness.Strict,
        wordBreak = LineBreak.WordBreak.Default
    )
)

private val demoText = "This is an example text\n今日は自由が丘で焼き鳥を食べます。"
private val presetNameStyle = SpanStyle(fontWeight = FontWeight.Bold, fontSize = 16.sp)

@OptIn(ExperimentalTextApi::class)
@Composable
fun TextLineBreakingDemo() {
    val selectedFontSize = remember { mutableStateOf(16f) }

    Column(modifier = Modifier.fillMaxSize()) {
        Text("Font size: ${selectedFontSize.value}")
        Slider(
            value = selectedFontSize.value,
            onValueChange = { value -> selectedFontSize.value = value },
            valueRange = 8f..48f
        )

        Row(Modifier.fillMaxWidth()) {
            val textModifier = Modifier
                .wrapContentHeight()
                .padding(horizontal = 5.dp)
                .border(1.dp, Color.Gray)

            lineBreakOptions.forEach { (presetName, preset) ->
                Text(
                    text = buildAnnotatedString {
                        withStyle(presetNameStyle) {
                            append(presetName)
                            append(":\n")
                        }
                        append(demoText)
                    },
                    style = TextStyle(
                        lineBreak = preset,
                        fontSize = selectedFontSize.value.sp
                    ),
                    modifier = textModifier.weight(1f)
                )
            }
        }
    }
}